package cn.le.utils;

import okhttp3.*;
import java.io.IOException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.List;
import java.net.*;
import java.util.concurrent.TimeUnit;

public class OkHttpUtil {

    private static final OkHttpClient okHttpClient;

    static {
        ConnectionPool pool = new ConnectionPool(5, 10, TimeUnit.MINUTES);
        okHttpClient = new OkHttpClient.Builder() //
                .connectTimeout(10, TimeUnit.SECONDS) //
                .followRedirects(false) //
                .followSslRedirects(false)
                .readTimeout(10, TimeUnit.SECONDS) //
                .retryOnConnectionFailure(false) //
                .writeTimeout(10, TimeUnit.SECONDS) //
                .build();
    }

    public static String get(Request request) throws IOException {
        Response response = okHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            //处理response的响应消息
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    public static String get(String url) throws IOException {
        Request request = new Request.Builder().url(url).get().build();
        return get(request);
    }

    public static void main(String[] args) throws IOException {
        Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("182.61.176.42",3128));
        try {
            URLConnection httpCon = new URL("https://sf.caa123.org.cn/pages/lots.html?&province=%E5%9B%9B%E5%B7%9D%E7%9C%81&lotStatus=&standardType=6&canLoan=&isRestricted=").openConnection(proxy);
            httpCon.setConnectTimeout(5000);
            httpCon.setReadTimeout(5000);
            int code = ((HttpURLConnection) httpCon).getResponseCode();
            System.out.println(code);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




}
